package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class CreateArticleServlet {
    public void service(HttpRequest request, HttpResponse response){
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        System.out.println(title+","+author+","+content);
        if (title==null||author==null||content==null){
            response.setEntity(
                    new File("./webapps/myweb/classTable.html"));
        }else {
            try(
                    ObjectOutputStream oos = new ObjectOutputStream(
                            new FileOutputStream("./articles/"+title+".obj"))
            ){
                Article article = new Article(title,author,content);
                oos.writeObject(article);
                File file = new File(
                        "./webapps/myweb/createArticle_success.html");
                response.setEntity(file);
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<");
            }catch(IOException e){
                e.printStackTrace();
            }
        }



    }
}
